package com.tfh.strategy;

import cn.hutool.core.date.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class RentAmount {
    Date start;
    Date end;
    BigDecimal amount;

    public RentAmount(BigDecimal amount, Date start, Date end) {
        this.amount = amount;
        this.start = start;
        this.end = end;
    }

    private long period() {
        return new DateBetween(end, start).between(DateUnit.DAY) + 1;
    }

    private long months() {
        return new DateBetween(start, end).betweenMonth(true);
    }

    public BigDecimal dayAmount() {
        long days = this.period();
        return amount.divide(BigDecimal.valueOf(days), RoundingMode.HALF_UP);
    }

    public BigDecimal monthAmount() {
        long months = this.months();
        return amount.divide(BigDecimal.valueOf(months), RoundingMode.HALF_UP);
    }

    private long calcDays(long index) {
        int offset = (int) index;
        Date monthStart = DateUtil.beginOfMonth(DateUtil.offsetMonth(start,offset));
        Date monthEnd = DateUtil.endOfMonth(DateUtil.offsetMonth(start,offset));
        DateRange month = new DateRange(monthStart,monthEnd,DateField.DAY_OF_MONTH);
        DateRange range = new DateRange(start,end, DateField.DAY_OF_MONTH);
        final List<DateTime> dateTimes = DateUtil.rangeContains(month, range);
        return dateTimes.size();
    }

    public List<Long> dayRange() {
        long months = this.months()+1;
        return LongStream.range(0, months)
                .mapToObj(this::calcDays)
                .collect(Collectors.toList());
    }

    public List<Long> range() {
        long months = this.months();
        return LongStream.range(0, months).mapToObj((long l) -> Long.valueOf(1)).collect(Collectors.toList());
    }


}
